package tcp.command;

import handlers.DataCallBack;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import constants.NetworkConstants;

public class CommandClient {

	public static void sendCommand(String commandAction,String serverId,String commandServer,int commandPort, DataCallBack data, String[] params){
		
		if((commandAction.equalsIgnoreCase(NetworkConstants.COMMAND_ACTION_UPLOAD_FILE)||commandAction.equalsIgnoreCase(NetworkConstants.COMMAND_ACTION_FILE_DELETE)||commandAction.equalsIgnoreCase(NetworkConstants.COMMAND_ACTON_STATUS) ||commandAction.equalsIgnoreCase(NetworkConstants.COMMAND_ACTION_CLIENT_UPDATE)|| commandAction.equalsIgnoreCase(NetworkConstants.COMMAND_ACTION_REPLICATE) || commandAction.equalsIgnoreCase(NetworkConstants.COMMAND_ACTION_FILE_MOVE))){
			BufferedReader in = null;
			PrintWriter out = null;
			try {
				Socket client = new Socket() ;
				client.connect(new InetSocketAddress(commandServer,commandPort)) ;
				out = new PrintWriter(client.getOutputStream(),true);
				in = new BufferedReader(new InputStreamReader(client.getInputStream()));
				if(commandAction.equalsIgnoreCase(NetworkConstants.COMMAND_ACTON_STATUS)){
					out.println(NetworkConstants.COMMAND_ACTON_STATUS);
					StringBuffer sb = new StringBuffer();
					String lineRead = null;
					while((lineRead = in.readLine()) != null && !lineRead.equalsIgnoreCase("EOF")){
						
						if(!lineRead.equalsIgnoreCase("STATUS OK")){
								sb.append(lineRead);
						}
					}
					if(data != null){
						data.dataResponse(serverId,sb.toString());
					}else{
						//No call back dump to screen
						System.out.println(sb.toString());
					}
				}
				if(commandAction.equalsIgnoreCase(NetworkConstants.COMMAND_ACTION_REPLICATE)){
					//Only need to check params parameter here and in file move code
					out.println(NetworkConstants.COMMAND_ACTION_REPLICATE);
					for(int i = 0; i < params.length; i++){
						out.println(params[i]);
					}
					out.println("EOF");
				}
				if(commandAction.equalsIgnoreCase(NetworkConstants.COMMAND_ACTION_FILE_MOVE)){
					//TODO Next release
				}
				if(commandAction.equalsIgnoreCase(NetworkConstants.COMMAND_ACTION_FILE_DELETE)){
					out.println(NetworkConstants.COMMAND_ACTION_FILE_DELETE);
					out.println(params[0]);
					out.println("EOF");
				}
				if(commandAction.equalsIgnoreCase(NetworkConstants.COMMAND_ACTION_CLIENT_UPDATE)){
					out.println(NetworkConstants.COMMAND_ACTION_CLIENT_UPDATE);
					out.println(params[0]);
					out.println("EOF");
				}
				if(commandAction.equalsIgnoreCase(NetworkConstants.COMMAND_ACTION_UPLOAD_FILE)){
					out.println(NetworkConstants.COMMAND_ACTION_UPLOAD_FILE);
					out.println(params[0]);
					out.println(params[1]);
					out.println("EOF");
				}
				out.close();
				in.close();
				client.close();
			} catch(java.net.ConnectException e){
				if(commandAction.equalsIgnoreCase(NetworkConstants.COMMAND_ACTION_CLIENT_UPDATE)){
					data.dataResponse(commandServer+":"+commandPort, "DELETE-CLIENT");
				}
			}catch (UnknownHostException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block

				e.printStackTrace();
			}
	
		}
	}
}
